package cn.rojao.wx.config.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import cn.rojao.wx.common.interceptor.AuthInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by zhenglian on 2016/10/12.
 */
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    /**
     * session区域解析器
     * @return
     */
//    @Bean
//    public LocaleResolver localeResolver() {
//        SessionLocaleResolver resolver = new SessionLocaleResolver();
//        //这里通过设置China.US可以进行中英文转化
//        resolver.setDefaultLocale(Locale.US);
//
//        return resolver;
//    }


    /**
     * cookie区域解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        //设置默认区域,
        slr.setDefaultLocale(Locale.CHINA);
        slr.setCookieMaxAge(3600);//设置cookie有效期.
        return slr;
    }

//    @Bean
//    public LocaleResolver localeResolver() {
//        FixedLocaleResolver slr = new FixedLocaleResolver ();
//        //设置默认区域,
//        slr.setDefaultLocale(Locale.US);
//        return slr;
//    }


    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 设置请求地址的参数,默认为：locale
//          lci.setParamName(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);
        return lci;
    }
    
    @Bean
    public HandlerInterceptor getMyInterceptor(){
	     List<String> excludeUrls = new ArrayList<String>();
	     excludeUrls.add("login/checkUser");
        return new AuthInterceptor(excludeUrls);
    }

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        /** 使用shiro时注释**/
       // registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
    }
    
    /**
     * 静态资源Handlers 
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
    	registry.addResourceHandler("/static/**").addResourceLocations("/static/");  
    } 

    
    /**  
     * Handles favicon.ico requests assuring no <code>404 Not Found</code> error is returned.  
     */  
    @Controller  
    static class FaviconController {  
        @RequestMapping("favicon.ico")  
        String favicon() {  
            return "forward:/resources/images/favicon.ico";  
        }  
    }
}
